<?php

namespace app\api\controller\wanlshop;

use app\common\controller\Api;
use think\Db;

/**
 *绿积分
 */
class Greenjifen extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
    /**
     * 绿积分
     */
    public function list()
    {
        $start_time = $this->request->post('start_time');
        $end_time = $this->request->post('end_time');
		$where['g.user_id']=$this->auth->id;
		if(!empty($start_time)&&!empty($end_time)){
           $where['g.createtime']=['between',[strtotime($start_time),strtotime($end_time)]];
		}

		$this->request->filter(['strip_tags']);
		$list = model('app\common\model\Greenjifenlog')
		    ->alias('g')
            ->join('wanlshop_order r','g.order_id = r.id')
			->field('r.order_no,g.createtime,g.value')
			->where($where)
			->where('value','GT',0)
			->order('g.updatetime desc')
			->paginate();
		$this->success('返回成功', ['allgreen' =>model('app\common\model\Greenjifenlog')->where('user_id',$this->auth->id)->sum("value") ,'list' => $list]);
	}

	 /**
     * 兑换券
     */
    public function greenCoupon()
    {
		$start_time = $this->request->post('start_time');
        $end_time = $this->request->post('end_time');
		$where['a.user_id']=$this->auth->id;
		if(!empty($start_time)&&!empty($end_time)){
           $where['a.createtime']=['between',[strtotime($start_time),strtotime($end_time)]];
		}

		$this->request->filter(['strip_tags']);
		$list = model('app\common\model\ConsumCard')
		        ->alias('a')
                ->join('wanlshop_greenjifenlog g','a.order_id=g.order_id and a.user_id=g.user_id')
                ->join('wanlshop_order r','r.id=a.order_id')
				->field('r.order_no,a.createtime,g.value,a.money')
				->where($where)
				->where('value','LT',0)
				->order('a.updatetime desc')
				->paginate();
		$this->success('返回成功', $list);
	}
   /**
	 * 计划任务绿兑换券
	 */
	public function allocation()
	{
        if(!cache('basicprofit')){
           cache('basicprofit',get_addon_config('wanlshop')['config']['working_hours']); 
           cache('nub',0);
        } 
		// 查询订单是否存在
        $paymoney = model('app\api\model\wanlshop\Pay')
            ->where('pay_state', 'EQ', '1')
            ->sum("actual_payment");
        if ($paymoney > cache('basicprofit')*115/100) {
            $numonearr = model('app\common\model\Greenjifenlog')->where('numbatch',0)->where('value','GT',0)->select();
            if ($numonearr) {
                foreach ($numonearr as $k => $v) {
                    // 兑换券
                    model('app\common\model\ConsumCard')->save([
                        'user_id' => $v['user_id'],
                        'order_id' => $v['order_id'],
                        'batch' => 1,
                        'typename' => "green",
                        'money' => $v['value']*5/1000,//model('\app\common\model\User')->where('id',model('app\api\model\wanlshop\Shop')->where('id',model('app\api\model\wanlshop\Order')->where('id',$v['order_id'])->value("shop_id"))->value("user_id"))->value("jifenuser_greenmultiple")
                    ]);
                    // 积分减
                    model('app\common\model\Greenjifenlog')->save([
                        'user_id' => $v['user_id'],
                        'value' => -round($v['value']/5,2),
                        'role' => $v['role'],
                        'order_id' => $v['order_id']
                    ]);
                    model('app\common\model\Greenjifenlog')->where('id',$v['id'])->update(['numbatch' => $v['numbatch']+1]);
                    model('app\common\model\User')->where('id',$v['user_id'])->dec('jifenuser_green',round($v['value']/5,2))->update();
                }
            }
            
            $afteronearr = model('app\common\model\Greenjifenlog')->where('numbatch','NEQ',0)->where('value','GT',0)->select();
            if ($afteronearr) {
                foreach ($afteronearr as $m => $n) {
                    // 兑换券
                    model('app\common\model\ConsumCard')->save([
                        'user_id' => $n['user_id'],
                        'order_id' => $n['order_id'],
                        'batch' => $n['numbatch']+1,
                        'typename' => "green",
                        'money' => model('app\common\model\ConsumCard')->where(['user_id' => $n['user_id'],'order_id' => $n['order_id'],'batch' => $n['numbatch']])->value("money")*1.15,//model('\app\common\model\User')->where('id',model('app\api\model\wanlshop\Shop')->where('id',model('app\api\model\wanlshop\Order')->where('id',$v['order_id'])->value("shop_id"))->value("user_id"))->value("jifenuser_greenmultiple")
                    ]);
                    // 积分减
                    model('app\common\model\Greenjifenlog')->save([
                        'user_id' => $n['user_id'],
                        'value' => -round($n['value']/5,2),
                        'role' => $n['role'],
                        'order_id' => $n['order_id']
                    ]);
                    model('app\common\model\Greenjifenlog')->where('id',$n['id'])->update(['numbatch' => $n['numbatch']+1]);
                    model('app\common\model\User')->where('id',$n['user_id'])->dec('jifenuser_green',round($n['value']/5,2))->update();
                }
            }
            
            $nub = cache('nub');
            cache('nub',$nub++);
            cache('basicprofit',cache('basicprofit')*115/100*cache('nub'));
        }

        if(!$paymoney) $this->error(__('非合理请求'));
	}
}